﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using HP.Cachalote.DataModels;
using HP.Cachalote.Repositories.Interfaces;
namespace HP.Cachalote.Repositories.EntityFramework
{
    public class TodoLabelRepository : RepositoryBase<TodoLabel>, ITodoLabelRepository
    {
        public TodoLabelRepository(CachaloteContext dataContext) : base(dataContext) { }

        /// <summary>
        /// Add new entity.
        /// </summary>
        /// <param name="entity">The new entity will be added into database.</param>
        /// <returns>The id of the new added entity.</returns>
        public virtual object Add(TodoLabel entity)
        {
            DataContext.TodoLabels.Add(entity);
            return entity;
        }

        // <summary>
        /// Get entity by id.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// <returns>The matched data.</returns>
        public virtual TodoLabel Get(object id)
        {
            return DataContext.TodoLabels.Where(label => label.ID == (int)id).FirstOrDefault();
        }

        /// <summary>
        /// Get entity by id. It will never return null. It will always return an entity or throw an exception.
        /// It is free to return a proxy instead. If you know that the value exist in the database, 
        /// and you don’t want to pay the extra select to have that, 
        /// but you want to get that value so we can add that reference to an object.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// <returns>The matched data.</returns>
        public virtual TodoLabel Load(object id)
        {
            return Get(id);
        }

        /// <summary>
        /// Filter data by lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <returns>An IQueryable list of data.</returns>
        public virtual IQueryable<TodoLabel> Filter(Expression<Func<TodoLabel, bool>> expression)
        {
            return DataContext.TodoLabels.Where(expression).AsQueryable();
        }

        /// <summary>
        /// Get all data. You also can filter the data later since the returned result is a IQueryable collection.
        /// </summary>
        /// <returns>An IQueryable collection of data.</returns>
        public virtual IQueryable<TodoLabel> GetAll()
        {
            return DataContext.TodoLabels.AsQueryable();
        }

        /// <summary>
        /// Get the data's count by using provided lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <returns>The data's count.</returns>
        public virtual int GetRowCount(Expression<Func<TodoLabel, bool>> expression)
        {
            return DataContext.TodoLabels.Where(expression).Count();
        }

        /// <summary>
        /// Update existing entity.
        /// </summary>
        /// <param name="entity">The entity will be updated.</param>
        public virtual void Update(TodoLabel entity)
        {
            DataContext.TodoLabels.Attach(entity);
        }

        /// <summary>
        /// Add or update entity.
        /// </summary>
        /// <param name="data">The entity will be updated if it is existing or added if it cannot be found.</param>
        public virtual void AddOrUpdate(TodoLabel entity)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete the specified entity.
        /// </summary>
        /// <param name="entity">The entity will be deleted from database.</param>
        public virtual void Delete(TodoLabel entity)
        {
            DataContext.TodoLabels.Remove(entity);
        }
    }
}
